package com.umeng.soexample.mvp.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.SeeListEntity;
import com.umeng.soexample.di.compont.DaggerSeeListElseFragmentCompont;
import com.umeng.soexample.di.modules.SeeListElseFragmentModules;
import com.umeng.soexample.mvp.contract.SeeListElseFragmentContract;
import com.umeng.soexample.mvp.persenter.SeeListElseFragmentPresenter;
import com.umeng.soexample.mvp.view.adapter.SeeListAdapter;

/**
 * 逛列表其他
 */
public class SeeListElseFragment extends BaseFragment<SeeListElseFragmentPresenter> implements SeeListElseFragmentContract.SeeListElseFragmentIView, BaseRefreshListener {

    private RecyclerView rv_see_list_else;
    private PullToRefreshLayout ptl_see_list_else;
    private SeeListAdapter adapter;
    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_see_list_else, null);
        return view;
    }

    @Override
    public void init(View view) {
        ptl_see_list_else = view.findViewById(R.id.ptl_see_list_else);

        //列表控件初始化
        rv_see_list_else = view.findViewById(R.id.rv_see_list_else);
        rv_see_list_else.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SeeListAdapter(R.layout.item_see_list);
        rv_see_list_else.setAdapter(adapter);
    }

    @Override
    public void initData() {
        p.request();
    }

    @Override
    public void setupDagger() {
        DaggerSeeListElseFragmentCompont.builder().seeListElseFragmentModules(new SeeListElseFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        ptl_see_list_else.setRefreshListener(this);
        //数据点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), ((SeeListAdapter)adapter).getData().get(position).see_news_title, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void success(SeeListEntity entity) {
        if (entity.values == null || entity.values.size() == 0){
            Toast.makeText(getActivity(), "没数据了", Toast.LENGTH_SHORT).show();
            return;
        }
        Log.e("tt", "SeeListElseFragment -> success: -> " + entity.values.size() );
        for (int i = 0; i < entity.values.size(); i++){
            entity.values.get(i).banner = entity.banner;
        }
        adapter.addData(entity.values);
    }


    @Override
    public void refresh() {
        adapter.getData().clear();
        p.refresh();
        ptl_see_list_else.finishRefresh();
    }

    @Override
    public void loadMore() {
        p.loadMore();
        ptl_see_list_else.finishLoadMore();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }
}
